/*
 * @描述信息: 保存图片到手机本地相册
 * @Author:  hunk
 */
export const saveImageToPhone = url => {
  return new Promise((reslove, reject) => {
    if (!url) {
      uni.showToast({
        title: '图片不存在，无法保存',
        icon: 'none'
      })
      reject(false)
    } else {
      uni.showLoading({
        title: '保存中...',
        mask: true
      })
      uni.saveImageToPhotosAlbum({
        filePath: url,
        success: () => {
          reslove(true)
        },
        fail: err => {
          if (err.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
            uni.showModal({
              title: '提示',
              content: '需要您授权保存图片到相册',
              success: res => {
                console.log(111, res)
                if (res.confirm) {
                  uni.openSetting({
                    success: settingdata => {
                      if (settingdata.authSetting['scope.writePhotosAlbum']) {
                        uni.showModal({
                          title: '提示',
                          content: '获取权限成功,再次点击即可保存',
                          showCancel: false
                        })
                      } else {
                        uni.showModal({
                          title: '提示',
                          content: '获取权限失败，将无法保存到相册哦~',
                          showCancel: false
                        })
                      }
                    }
                  })
                } else {
                  uni.showToast({
                    title: '拒绝授权',
                    icon: 'none',
                    duration: 1000
                  })
                }
              }
            })
          }
          reject(err)
        },
        complete: () => {
          uni.hideLoading()
        }
      })
    }
  })
}
